from pydantic import BaseModel, Field, validator

class UserCreate(BaseModel):
    username: str = Field(..., description="用户名")
    password: str = Field(..., description="密码")
    
    @validator('username')
    def usernameMustBeValid(cls, v):
        if not v or len(v) < 3:
            raise ValueError('用户名长度不能少于3个字符')
        if len(v) > 20:
            raise ValueError('用户名长度不能超过20个字符')
        return v
    
    @validator('password')
    def passwordMustBeValid(cls, v):
        if not v or len(v) < 6:
            raise ValueError('密码长度不能少于6个字符')
        if len(v) > 50:
            raise ValueError('密码长度不能超过50个字符')
        return v

class UserLogin(BaseModel):
    username: str = Field(..., description="用户名")
    password: str = Field(..., description="密码")

class UserOut(BaseModel):
    id: str
    username: str
    permission: str
    createdAt: str
    updatedAt: str

class TokenSchema(BaseModel):
    accessToken: str
    refreshToken: str

class LoginResponse(BaseModel):
    user: UserOut
    accessToken: str
    refreshToken: str

class RegisterResponse(BaseModel):
    user: UserOut
    accessToken: str
    refreshToken: str

class TokenPayload(BaseModel):
    sub: str
    exp: int

class RefreshTokenRequest(BaseModel):
    refreshToken: str
    
class LogoutRequest(BaseModel):
    token: str = Field(..., description="要注销的访问令牌") 